<%@ page import="java.sql.*" %>
<%@ page import="com.mysql.cj.jdbc.Driver" %><%--
  Created by IntelliJ IDEA.
  User: ASUS
  Date: 2025/3/8
  Time: 10:59
  To change this template use File | Settings | File Templates.
--%>
<%@ page contentType="text/html;charset=UTF-8" language="java" %>
<html>
<head>
    <title>图书列表</title>
</head>
<body>
<table border="1">
    <tr>
        <th>id</th>
        <th>name</th>
        <th>date</th>
        <th>price</th>
    </tr>
<%
    Connection conn = null;
    Statement stmt = null;
    ResultSet rs=null;
    try {
        DriverManager.registerDriver(new Driver());
        //获取连接
        conn = DriverManager.getConnection("jdbc:mysql://localhost:3306/jdbc","root","123456");
        //
        String sql = "select * from t_book";
        stmt=conn.createStatement();
        rs=stmt.executeQuery(sql);
        while (rs.next()){
            %>
    <tr>
        <td><%=rs.getInt("id")%></td>
        <td><%=rs.getString("name")%></td>
        <td><%=rs.getDate("date")%></td>
        <td><%=rs.getDouble("price")%></td>
    </tr>
    <%
        }
    } catch (Exception e) {
        e.printStackTrace();
    }finally {
        if (rs!=null){
            try {
                rs.close();
            } catch (SQLException e) {
                throw new RuntimeException(e);
            }
        }
        if (stmt!=null){
            try {
                stmt.close();
            } catch (SQLException e) {
                throw new RuntimeException(e);
            }
        }
        if (conn!=null){
            try {
                conn.close();
            } catch (SQLException e) {
                throw new RuntimeException(e);
            }
        }
    }
%>
</table>
</body>
</html>
